﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;


namespace Asomy.Model
{
    class Layer
    {
        public int m_width;
        public int m_height;
        public Color[,] m_pixels;        
        private bool m_visible = true;
        private bool m_locked = false;
        public string m_name;
        public double m_opacity = 1;

        public int m_id;

        public bool Visible
        {
            get { return m_visible; }
        }

        public bool Locked
        {
            get { return m_locked; }
        }

        /// <summary>
        /// Creates the background layer of gray &  black squares.
        /// </summary>        
        public Layer(int a_width, int a_height)
        {
            m_width = a_width;
            m_height = a_height;

            m_name = "";

            m_pixels = new Color[m_width, m_height];

            int f_occurence = 15;
            int f_half = f_occurence / 2;

            for (int y = 0; y < m_height; y++)
            {
                for (int x = 0; x < m_width; x++)
                {                    
                    if (y % f_occurence >= f_half)
                    {
                        if (x % f_occurence >= f_half)
                        {
                            m_pixels[x, y] = Color.FromArgb(75, 0, 0, 0);
                        }
                        else
                        {
                            m_pixels[x, y] = Color.FromArgb(175, 0, 0, 0);
                        }
                    }
                    else
                    {
                        if (x % f_occurence >= f_half)
                        {
                            m_pixels[x, y] = Color.FromArgb(175, 0, 0, 0);
                        }
                        else
                        {                            
                            m_pixels[x, y] = Color.FromArgb(75, 0, 0, 0);
                        }
                    }                      
                }
            }   
        }

        public Layer(int a_width,int a_height,string a_name)
        {
            m_width = a_width;
            m_height = a_height;           
            
            m_pixels = new Color[m_width, m_height];

            m_name = a_name;           
            
            for (int x = 0; x < m_width; x++)
            {
                for (int y = 0; y < m_height; y++)
                {                    
                    m_pixels[x, y] = new Color();
                }
            }            
        }

        public void Show()
        {
            m_visible = true;
        }

        public void Hide()
        {
            m_visible = false;
        }

        public void Lock()
        {
            m_locked = true;
        }

        public void UnLock()
        {
            m_locked = false;
        }

        public void ChangeOpacity(double a_opacity)
        {
            m_opacity = a_opacity;
            if (m_opacity > 1)
            {
                m_opacity = 1;
            }
            else if (m_opacity < 0)
            {
                m_opacity = 0d;
            }
        }

        /// <summary>
        /// Sets all pixels in the layer to 1 color
        /// </summary>
        /// <param name="a_color">The color you want all pixels to get</param>
        public void UpdateAllPixels(Color a_color)
        {
            for (int x = 0; x < m_width; x++)
            {
                for (int y = 0; y < m_height; y++)
                {
                    m_pixels[x, y] = a_color;
                }
            }
        }

        /// <summary>
        /// Changes a specific pixels color
        /// </summary>
        /// <param name="a_x">What pixel in x-axis</param>
        /// <param name="a_y">What pixel in y-axis</param>
        /// <param name="a_color">The new color</param>
        public bool ChangePixelColor(int a_x, int a_y,Color a_color)
        {            
            if (a_x < m_width && a_x >= 0 && a_y < m_height && a_y >= 0 && !Locked && Visible && a_color != m_pixels[a_x,a_y])
            {
                //float f_alpha = a_color.ScA;
                //Color.Add(Color.Multiply(m_pixels[a_x,a_y], 1 - f_alpha), Color.Multiply(a_color, f_alpha)); ;

                m_pixels[a_x, a_y] = a_color;
                return true;
            }
            return false;
        }

        
    }
}
